//
//  WaybillManagerGoodListVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/16.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "WaybillManagerGoodListVC.h"
#import "GoodListCell.h"
#import "GoodListSectionHeadView.h"

@interface WaybillManagerGoodListVC ()<UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UILabel *weightLab;
@property (nonatomic, strong) UILabel *volumeLab;
@property (nonatomic, strong) UILabel *pieceLab;
@property (nonatomic, strong) UILabel *countLab;

@end

@implementation WaybillManagerGoodListVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 100, ScreenW, 100 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, ScreenW - 30, 15)];
        titleLab.text = @"运输计划总计";
        titleLab.font = BoldFontOfSize(14);
        titleLab.textColor = HexColor(0x2B2C44);
        [_bottomView addSubview:titleLab];
        
        self.weightLab = [[UILabel alloc] initWithFrame:CGRectMake(15, (100 - 35 - 15 - 15) * 0.5 + titleLab.nim_bottom, (ScreenW - 30) * 0.5, 15)];
        self.weightLab.textColor = HexColor(0x9C9DB3);
        self.weightLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.weightLab];
        
        self.volumeLab = [[UILabel alloc] initWithFrame:CGRectMake(self.weightLab.nim_right, self.weightLab.nim_top, self.weightLab.nim_width, self.weightLab.nim_height)];
        self.volumeLab.textColor = HexColor(0x9C9DB3);
        self.volumeLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.volumeLab];
        
        self.pieceLab = [[UILabel alloc] initWithFrame:CGRectMake(self.weightLab.nim_left, self.weightLab.nim_bottom + 5, self.weightLab.nim_width, self.weightLab.nim_height)];
        self.pieceLab.textColor = HexColor(0x9C9DB3);
        self.pieceLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.pieceLab];
        
        self.countLab = [[UILabel alloc] initWithFrame:CGRectMake(self.pieceLab.nim_right, self.pieceLab.nim_top, self.weightLab.nim_width, self.weightLab.nim_height)];
        self.countLab.textColor = HexColor(0x9C9DB3);
        self.countLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.countLab];
    }
    return _bottomView;
}

-(NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSArray array];
    }
    return _titleArray;
}

-(NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height)];
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, self.scrollView.nim_height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForData];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"货物清单";
    page = 1;
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryWaybillGoodList params:@{@"wa_id":self.wa_id,@"page":@(page),@"type":self.type} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            NSDictionary *dd = [responseObject objectForKey:@"list"];
            self.titleArray = [dd objectForKey:@"header"];
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [NSMutableArray arrayWithArray:dd[@"list"]];
            }else {
                NSArray *array = dd[@"list"];
                [self.array addObjectsFromArray:array];
            }
            self.weightLab.text = [NSString stringWithFormat:@"重量(kg)：%@",[[responseObject objectForKey:@"total_info"] objectForKey:@"allWeight"]];
            self.volumeLab.text = [NSString stringWithFormat:@"体积(m³)：%@",[[responseObject objectForKey:@"total_info"] objectForKey:@"allVolume"]];
            self.pieceLab.text = [NSString stringWithFormat:@"件数：%@",[[responseObject objectForKey:@"total_info"] objectForKey:@"goodsJnum"]];
            self.countLab.text = [NSString stringWithFormat:@"货物数量：%@",[[responseObject objectForKey:@"total_info"] objectForKey:@"goodsNum"]];
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self configFrame];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)configFrame {
    NSArray *dic = self.array.firstObject;
    self.scrollView.contentSize = CGSizeMake(GoodList_ViewWidth * dic.count, self.scrollView.bounds.size.height);
    self.tableView.nim_width = self.scrollView.contentSize.width;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GoodListSectionHeadView *view = [[GoodListSectionHeadView alloc] initWithFrame:CGRectMake(0, 0, GoodList_ViewWidth * self.titleArray.count, 45)];
    view.titleArray = self.titleArray;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodListCell *cell = [GoodListCell GoodListCellWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodListCell *cell = [GoodListCell GoodListCellWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    return cell.height;
}

@end
